<template>
  <section class="login">
    <div>
      <img src="../assets/img/login.jpg" alt="" />
      <!-- form表单 -->
      <el-form
        ref="loginFormRef"
        :rules="loginFormRules"
        :model="loginForm"
        class="login-form"
      >
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            prefix-icon="el-icon-user-solid"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            prefix-icon="el-icon-s-cooperation"
            show-password
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </section>
</template>
<script>
// 导入封装网络请求模块
import { requestPost } from "../request/request";
export default {
  name: "",
  data() {
    return {
      loginForm: {
        username: "admin",
        password: "123456"
      },
      // 表单验证
      loginFormRules: {
        username: [
          {
            required: true,
            message: "请输入用户名称",
            trigger: "blur"
          },
          {
            min: 3,
            max: 10,
            message: "长度在3到10字符之间",
            trigger: "blur"
          }
        ],
        password: [
          {
            required: true,
            message: "请输入密码",
            trigger: "blur"
          },
          {
            min: 6,
            max: 15,
            message: "长度在6到15字符之间",
            trigger: "blur"
          }
        ]
      }
    };
  },
  methods: {
    // 重置
    resetLoginForm() {
      this.$refs.loginFormRef.resetFields();
    },
    //登录验证
    login() {
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) return;
        const { data: res } = await requestPost("login", this.loginForm);
        console.log(res);
        if (res.meta.status != 200) return this.$message.error(res.meta.msg);
        this.$message.success(res.meta.msg);
        window.sessionStorage.setItem("token", res.data.token);
        this.$router.push("/home");
      });
    }
  }
};
</script>
<style lang="scss" scoped>
$backgroundColor: #1f374e;
.login {
  background-color: $backgroundColor;
  height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  > div {
    background-color: #fff;
    width: 450px;
    height: 300px;
    border-radius: 15px;
    position: relative;
    img {
      width: 90px;
      height: 90px;
      border-radius: 50%;
      border: 6px solid #fff;
      box-shadow: 0 0 10px rgb(214, 213, 213);
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      z-index: 1;
    }
    .btns {
      display: flex;
      justify-content: flex-end;
    }
    .login-form {
      position: absolute;
      bottom: 0;
      width: 100%;
      padding: 0 20px;
    }
  }
}
</style>
